//
//  ViewController.m
//  UI22_Work
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
#define kTitleCollectionView @"firstReuse"
#import "TitleCollectionView.h"
#import "Cycle.h"
#import "CycleImage.h"
#import "FirstModel.h"
#import "MyCollectionViewCell.h"
#define kHead @"headReuse"

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *titleCollectionView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *collectionDataArray;
@end

@implementation ViewController

/** http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=json */


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCycleImageData];
    [self createTitleCollectionView];
    [self createCollectionView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2 , 220);
    layout.headerReferenceSize = CGSizeMake(7, 200);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 308, self.view.bounds.size.width - 20, self.view.bounds.size.height - 308) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.collectionView];

    [self.collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"secReuse"];

    // 分区头视图
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHead];
    
}



#if 1
- (void)createCycleImageData {
    NSURL *url = [NSURL URLWithString:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        str = [str substringFromIndex:7];
        str = [str substringToIndex:str.length - 1];
        data = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
       
        
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.collectionDataArray = [NSMutableArray array];
                    FirstModel *fir = [FirstModel modelWithDic:dic];
                    for (NSInteger i = 0; i < fir.data.contents.list.count; i++) {
                        for (NSInteger j = 0; j < fir.data.contents.list[i].mvlist.count; j++) {
//                            [self.collectionDataArray addObject:fir.data.contents.list[i].mvlist[j].picurl];
                        }
                    }
                    NSLog(@"xxxxxx %@", fir.data.contents.list[2].mvlist[2].picurl);

                    
        // 获取轮播图 数据
        CycleImage *cycle = [[CycleImage alloc] initWithFrame:CGRectMake(10, 108, self.view.bounds.size.width - 20, 200)];
        [self.view addSubview:cycle];
        NSMutableArray *mutArray = [NSMutableArray array];
        for (NSInteger i = 0; i < fir.data.mv_show_play.count; i++) {
            [mutArray addObject:fir.data.mv_show_play[i].picurl];;
        }
            NSString *first = [mutArray firstObject];
            NSString *last = [mutArray lastObject];
            [mutArray addObject:first];
            [mutArray insertObject:last atIndex:0];
            cycle.array = mutArray;
            });
    }];
    [task resume];
}
#endif


- (void)createTitleCollectionView {
    self.titleArray = [NSArray array];
    self.titleArray = @[@"精选", @"排行", @"歌单", @"电台", @"MV"];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if ([collectionView isEqual:self.titleCollectionView]) {
        return 1;
    } else {
        return 10;
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if ([collectionView isEqual:self.titleCollectionView]) {
        return self.titleArray.count;
    } else {
        return 4;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual:self.titleCollectionView]) {
        TitleCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.titleLabel.text = self.titleArray[indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    } else {
        
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secReuse" forIndexPath:indexPath];
//        [cell.picImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.collectionDataArray[indexPath.row]]]]];
//        NSLog(@"%@", self.collectionDataArray[indexPath.row]);
        cell.backgroundColor = [UIColor redColor];
        return cell;
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHead forIndexPath:indexPath];
    return reuseView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
